package com.hspedu.newData.dmsxl.哈希表;

import org.junit.Test;

import java.util.HashMap;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-22 
 * @Version: 1.0
 */

public class LC383赎金信 {


    // todo
    // 效率很低查查别的方法
    public boolean canConstruct(String ransomNote, String magazine) {

        HashMap<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < magazine.length(); i++) {
            map.put(magazine.charAt(i),map.getOrDefault(magazine.charAt(i),0)+1);
        }

        for (int i = 0; i < ransomNote.length(); i++) {
            if (!map.containsKey(ransomNote.charAt(i)) || map.get(ransomNote.charAt(i)) < 1){
                return false;
            }
            map.put(ransomNote.charAt(i),map.get(ransomNote.charAt(i)) - 1);
        }
        return true;
    }


    @Test
    public void test() {
        // 测试代码
    }
}
